﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [_Old_AllAttributes]

AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	DECLARE @allcols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX)

		select @allcols = STUFF((SELECT  ',' + QUOTENAME(AttributeName) 
						from DefinedAttributes 
						group by  AttributeName 
				FOR XML PATH(''), TYPE
				).value('.', 'NVARCHAR(MAX)') 
			,1,1,'')

		set @query = @query +
				' SELECT EntityID, ' + @allcols + '
					from AttributesWithAttributeNames
				pivot 
				(
					max(AttributeValueString)
					for AttributeName in (' + @allcols + ')
				) a '

    -- Insert statements for procedure here
	EXECUTE(@query)
END
